using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;


namespace MainGame.Menu
{
  public delegate void MenuEvent();

  public enum MenuDepths
  {
    Background,
    GameLayer,
    Overlay,
    BackMenu,
    MiddleMenu,
    FrontMenu,    
    ForeGround,
    numMenus
  }


  /// <summary>
  /// This is a game component that implements IUpdateable.
  /// </summary>
  public class BaseMenu : Microsoft.Xna.Framework.DrawableGameComponent
  {
    public struct ClickableButton 
    {
      public Button b;
      public Action on_click;
    }

    public List<ClickableButton> buttons { get; private set; }
    public Texture2D background { get; set; }
    public Rectangle bounds;
    SpriteBatch spriteBatch;
    public MenuDepths menuDepth;

    public BaseMenu(Game game, MenuDepths depth)
      : base(game)
    {
      spriteBatch = Game.GetContext().SpriteBatch;
      buttons = new List<ClickableButton>();
      this.menuDepth = depth;
      this.DrawOrder = (int)depth;
    }

    public override void Initialize()
    {
      var input_service = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
      input_service.OnTap += new InputManager.Tap(input_service_OnTap);

      base.Initialize();
    }

    protected override void Dispose(bool disposing)
    {
      base.Dispose(disposing);
    }

    protected override void LoadContent()
    {
      base.LoadContent();
    }

    protected void AddButton(Button b, Action on_click)
    {
      buttons.Add(new ClickableButton() { b = b, on_click = on_click });
    }

    private void input_service_OnTap(Vector2 tapPosition)
    {
      var screenTransform = Game.GetContext().ScreenTransform;
      foreach (var cb in buttons)
      {
        if(cb.b.IsInBounds(tapPosition.ToPoint(screenTransform)))
          cb.on_click();
      }
    }

    public override void Draw(GameTime gameTime)
    {
      spriteBatch.Begin(/*SpriteSortMode.FrontToBack, BlendState.AlphaBlend*/);


      if (background != null)
        spriteBatch.Draw(background, bounds, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, (float)DrawOrder/(float)MenuDepths.numMenus);
      float buttonsDepth = ((float)DrawOrder / (float)MenuDepths.numMenus) + 0.01f;
       
      foreach (Button b in (from cb in buttons select cb.b))
      {
        spriteBatch.Draw(b.texture, b.bounds, null, b.color, 0, Vector2.Zero, SpriteEffects.None, buttonsDepth);
        spriteBatch.DrawString(b.spriteFont, b.message, new Vector2((float)b.bounds.Center.X, (float)b.bounds.Center.Y), b.textColor, 0, b.spriteFont.MeasureString(b.message) / 2, 1.0f, SpriteEffects.None, buttonsDepth);
      }
      spriteBatch.End();

      base.Draw(gameTime);
    }
  }
}
